﻿module Sustainalytics.BlockUi {

    export class BlockUiBehavior {

        private controller: BlockUiController;

        constructor(
            private scope: BlockUiScope,
            private $timeout: ng.ITimeoutService,
            private element,
            private attrs) {

            var ctx = this;

            ctx.init();
        }

        private init(): void {

            var ctx = this;

            (<ng.IScope><any>ctx.scope).$watch('isVisible', function () {

                var el = angular.element(ctx.element);

                if (ctx.scope.isVisible)
                    el.addClass('sustainalytics-blockui-visible');
                else
                    el.removeClass('sustainalytics-blockui-visible');
            });
        }
    }


    var directive = function ($timeout) {

        return {
            // apply by class
            restrict: 'C',

            // generate child scope
            scope: {
                isVisible: '=isvisible'
            },

            // associate with controller
            controller: 'BlockUiController',

            // directive template URL
            templateUrl: 'Views/BlockUi.html',

            // whether to replace the target element with the given template
            replace: true,

            // link function for attaching event handlers and applying scope
            link: function (scope: BlockUiScope, element, attrs) {
                var behavior = new BlockUiBehavior(scope, $timeout, element, attrs);
            }
        };
    }


    angular.module(blockUiModuleName).directive('sustainalyticsBlockui', ['$timeout', directive]);    
}